import { isEqual } from "lodash";
import { Module, VuexModule, Action } from "vuex-class-modules";
import { getTaskList } from "../apis/Layout";

@Module({ generateMutationSetters: true })
export default class TaskStore extends VuexModule {
  tasks=[];
  /** 任务情况是否有变动 */
  hasMessage=false;

  @Action
  async refreshTaskInfo() {
    const {data} = await getTaskList();

    // debugger;
    // 判断任务长度和任务数据长度是否不一致
    if (this.tasks.length !== data.length) {
      this.hasMessage = true;
    } else if (isEqual(this.tasks, data)) {
      this.hasMessage = true;
    }
    this.tasks = data;
    return this.tasks;
  }
  @Action
  async updateTaskStatus({taskId, status, percentage}) {
    const task = this.tasks.find(f => f.taskId === taskId);
    if (task) {
      task.status = status;
      task.percentage = percentage;
    }
  }

  /**
   * 设置消息被点开查看过了
   */
  @Action
  async setMessageChecked() {
    this.hasMessage = false;
  }
}
